import Vue from "vue";
// Route类型需要自己从vue-router中引入
import Router, { Route } from "vue-router";

import Home from "../pages/Home.vue";

Vue.use(Router); //注册插件

let routes = [
  {
    path: "/home",
    name: "Home",
    component: Home,
  },
  {
    path: "/user",
    name: "User",
    component: () => import(/* webpackChunkName: "user" */ "../pages/User.vue"),
    beforeEnter: (to: Route, from: Route, next: Function) => {
      // ...
      console.log('进入User页面-页面独享的前置守卫')
      next()
    },
  },
  {
    path: "/login",
    name: "Login",
    component: (r: any) =>
      require([/* webpackChunkName: "login" */ "../pages/Login.vue"], r),
  },
  {
    path: "/goods",
    name: "Goods",
    component: () =>
      import(/* webpackChunkName: "goods" */ "../pages/Goods.vue"),
  },
  {
    path: "/details",
    name: "Details",
    component: () =>
      import(/* webpackChunkName: "details" */ "../pages/Details.vue"),
  },
  {
    path: "/notfound",
    name: "NotFound",
    component: () =>
      import(/* webpackChunkName: "NotFound" */ "../pages/NotFound.vue"),
  },
  { path: "", redirect: { name: "Home" } },
  { path: "/", redirect: { name: "Home" } },
  { path: "*", redirect: { name: "Home" } },
];

const router = new Router({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});
router.beforeEach((to, from, next) => {
  console.log("全局前置守卫(会拦截)-处理逻辑-记得放行");
  next();
});
router.afterEach((to, from) => {
  //没有参数next
  console.log("全局后置守卫-处理逻辑");
});
export default router;
